package com.jz.community.moduleshopping.goodsDetail.ui.view;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.bean.baseCoupon.BaseCouponInfo;
import com.jz.community.basecomm.task.GetReceiveCouponTask;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.confirmOrder.bean.BaseOrderCouponInfo;
import com.jz.community.moduleshopping.goodsDetail.adapter.GoodsCouponAdapter;

import java.util.List;

/**
 * Created by Tony.Fan on 2018/5/28 14:36
 * 商品详情领卷弹框
 */
public class GoodsDetailCouponPop implements View.OnClickListener {
    private Context mContext;
    private RecyclerView shopCouponRv;
    private ImageView closeIv;
    private View view;
    private BottomSheetDialog bottomSheetDialog;

    private GoodsCouponAdapter goodsCouponAdapter;


    public GoodsDetailCouponPop(Context context) {
        mContext = context;
        initView();
        addListener();
    }

    public void addCouponInfos(List<BaseOrderCouponInfo> contentBeans) {
        handleBindAdapter(contentBeans);
        showPopUp();
    }

    private void addListener() {
        closeIv.setOnClickListener(this);
    }

    public void showPopUp() {
        if (bottomSheetDialog == null) {
            bottomSheetDialog = new BottomSheetDialog(mContext);
            bottomSheetDialog.setContentView(view);
        }
        bottomSheetDialog.show();
    }

    public void initView() {
        view = LayoutInflater.from(mContext).inflate(R.layout.module_shopping_gt_coupon_bs, null);
        shopCouponRv = view.findViewById(R.id.gt_bs_rv);
        closeIv = view.findViewById(R.id.gt_bs_back);
    }

    private void handleBindAdapter(List<BaseOrderCouponInfo> contentBeans) {
        LinearLayoutManager main_layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        shopCouponRv.setLayoutManager(main_layoutManager);
        goodsCouponAdapter = new GoodsCouponAdapter(contentBeans);
        goodsCouponAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//加载动画
        shopCouponRv.setAdapter(goodsCouponAdapter);
        goodsCouponAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                if (view.getId() == R.id.coupon_gt_click_tv) {
                    if (goodsCouponAdapter.getData().get(position).getHistoryStatus() == 4) {
                        getReceiveCouponTask(goodsCouponAdapter.getData().get(position).getId(), position);
                    } else {
                        bottomSheetDialog.dismiss();
                    }
                }
            }
        });
    }

    /**
     * 领券请求
     *
     * @param couponId
     */
    private void getReceiveCouponTask(String couponId, int position) {
        new GetReceiveCouponTask((Activity) mContext, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                BaseCouponInfo baseCouponInfo = (BaseCouponInfo) o;
                if (Preconditions.isNullOrEmpty(baseCouponInfo)) {
                    return;
                }
                if (!Preconditions.isNullOrEmpty(baseCouponInfo.getCouponInfo())) {
                    WpToast.l(mContext, "领劵成功");
                    goodsCouponAdapter.getData().get(position).setHistoryStatus(3);
                    goodsCouponAdapter.notifyDataSetChanged();
                } else {
                    WpToast.l(mContext, baseCouponInfo.getMessage());
                }
            }
        }).execute(couponId);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.gt_bs_back) {
            bottomSheetDialog.dismiss();
        }
    }
}
